package indi.jp.project.business.jingying.service.impl;

import indi.jp.project.business.jingying.dao.ProjectTrackDAO;
import indi.jp.project.business.jingying.pojo.po.ProjectTrackPO;
import indi.jp.project.business.jingying.pojo.po.ProjectTrackRecordPO;
import indi.jp.project.business.jingying.pojo.query.ProjectTrackQuery;
import indi.jp.project.business.jingying.pojo.query.ProjectTrackRecordQuery;
import indi.jp.project.business.jingying.service.ProjectTrackService;
import indi.jp.project.business.pub.pojo.ProjectStatusEnum;
import indi.jp.project.business.pub.pojo.po.ProjectPO;
import indi.jp.project.business.pub.service.ProjectService;
import indi.jp.project.core.entity.Datagrid;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.util.SerialUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 项目跟踪
 *
 * @author jiangping
 * @date 2022/6/5
 */
@Service
public class ProjectTrackServiceImpl implements ProjectTrackService {

    @Resource
    private ProjectTrackDAO projectTrackDAO;

    @Resource
    private SerialUtil serialUtil;

    @Resource
    private ProjectService projectService;

    @Override
    public JsonResult queryGrid(ProjectTrackQuery query) throws Exception {
        List<ProjectTrackPO> list = projectTrackDAO.queryList(query);
        int count = projectTrackDAO.queryCount(query);
        Datagrid<ProjectTrackPO> datagrid = new Datagrid<>(list, count);
        return JsonResult.success(datagrid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult save(ProjectTrackPO projectTrackPO) throws Exception {
        ProjectPO projectPO = new ProjectPO();
        projectPO.setName(projectTrackPO.getProjectName());
        projectPO.setBusinessType(projectTrackPO.getBusinessType());
        projectPO.setAddrCode(projectTrackPO.getProjectAddrCode());
        projectPO.setAddrName(projectTrackPO.getProjectAddrName());
        projectPO.setAmountOfContract(projectTrackPO.getAmountOfContract());
        projectPO.setClientCompanyId(projectTrackPO.getClientCompanyId());
        projectPO.setClientCompanyName(projectTrackPO.getClientCompanyName());
        projectPO.setDepartmentId(projectTrackPO.getDepartmentId());
        projectPO.setDepartmentCode(projectTrackPO.getDepartmentCode());
        projectPO.setDepartmentName(projectTrackPO.getDepartmentName());
        projectPO.setBranchOffice(projectTrackPO.getOffice());
        projectPO.setCustomerManager(projectTrackPO.getCustomerManager());
        projectPO.setDivision(projectTrackPO.getDivision());
        projectPO.setMarketingDirector(projectTrackPO.getMarketingDirector());
        if (StringUtils.isBlank(projectTrackPO.getId())) {
            projectPO.setStatus(ProjectStatusEnum.IN_TRACE.getCode());
            projectPO.setStatusName(ProjectStatusEnum.IN_TRACE.getName());
            projectPO.setCreateBy(projectTrackPO.getCreateBy());
            String projectId = projectService.add(projectPO);
            String id = String.valueOf(serialUtil.nextId());
            projectTrackPO.setId(id);
            projectTrackPO.setProjectId(projectId);
            projectTrackDAO.insert(projectTrackPO);
        } else {
            projectPO.setId(projectTrackPO.getProjectId());
            projectPO.setModifyBy(projectTrackPO.getModifyBy());
            projectService.edit(projectPO);
            projectTrackDAO.update(projectTrackPO);
        }
        return JsonResult.success();
    }

    @Override
    public JsonResult findById(String id) throws Exception {
        ProjectTrackPO projectTrackPO = projectTrackDAO.findById(id);
        return JsonResult.success(projectTrackPO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult deleteById(String id) throws Exception {
        projectTrackDAO.deleteById(id);
        projectTrackDAO.deleteRecords(id);
        return JsonResult.success();
    }

    @Override
    public JsonResult queryRecordGrid(ProjectTrackRecordQuery query) throws Exception {
        List<ProjectTrackRecordPO> list = projectTrackDAO.queryRecordList(query);
        int count = projectTrackDAO.queryRecordCount(query);
        Datagrid<ProjectTrackRecordPO> datagrid = new Datagrid<>(list, count);
        return JsonResult.success(datagrid);
    }

    @Override
    public JsonResult saveRecord(ProjectTrackRecordPO projectTrackRecordPO) throws Exception {
        if (StringUtils.isBlank(projectTrackRecordPO.getId())) {
            String id = String.valueOf(serialUtil.nextId());
            projectTrackRecordPO.setId(id);
            projectTrackDAO.insertRecord(projectTrackRecordPO);
        } else {
            projectTrackDAO.updateRecord(projectTrackRecordPO);
        }
        return JsonResult.success();
    }

    @Override
    public JsonResult deleteRecord(String id) throws Exception {
        projectTrackDAO.deleteRecord(id);
        return JsonResult.success();
    }

}
